package com.fudan.backend.config;

import com.fudan.backend.interceptor.AdminInterceptor;
import com.fudan.backend.interceptor.LoginInterceptor;
import com.fudan.backend.interceptor.SellerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MvcConfig implements WebMvcConfigurer {
    @Autowired
    private LoginInterceptor loginInterceptor;

    @Autowired
    private AdminInterceptor adminInterceptor;

    @Autowired
    private SellerInterceptor sellerInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/user/login", "/user/register")
                .excludePathPatterns("/store/vo", "/store/vo/**", "/store/info", "/store/info/**")
                .excludePathPatterns("/commodity/store/**", "/commodity/activity/**", "/commodity/info", "/commodity/info/**")
                .excludePathPatterns("/category/info/**", "/category", "/category/search")
                .excludePathPatterns("/img/**")
                .excludePathPatterns("/activity/ongoing", "/activity/info/**");

        registry.addInterceptor(adminInterceptor)
                .addPathPatterns("/user/all")
                .addPathPatterns("/store/delete-request/process")
                .addPathPatterns("/store/delete-request");

        registry.addInterceptor(sellerInterceptor)
                .addPathPatterns("/store/open")
                .addPathPatterns("/store/delete-request/**")
                .addPathPatterns("/account/store**")
                .addPathPatterns("/account/webshop")
                .addPathPatterns("/activity/participate-request");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/img/**")
//                .addResourceLocations("file:D:/images/");
                .addResourceLocations("classpath:/images/");
    }

}
